// components/chat-history/chat-history.ts
import api from "../../api/index";
import common from "../../common/index";

const app = getApp()
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    topic: {
      topic_id: app.globalData.topic.topic_id,
      topic_name: app.globalData.topic.topic_name,
    },
    statusBarHeight: app.globalData.statusBarHeight,
    navBarHeight: app.globalData.navBarHeight,
    uid: '',
    historyList: [],
    cacheHistoryList: [],
    searchText: '',
    fetchLoading: false,
  },

  lifetimes: {
    attached() {
      const user = common.getAppUserInfo()
      this.data.uid = user.uid
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setHistoryDataGroup(historyData) {
      let tag = ''
      let len = 0
      this.data.historyList = []
      historyData.forEach((item) => {
        item.tag = item.tag || '今天'
        if (tag !== item.tag) {
          len = this.data.historyList.length
          this.data.historyList[len] = {
            tag: item.tag,
            list: [],
            edit: false,
          }
          tag = item.tag
        }
        this.data.historyList[len].list.push(item)
      })
    },
    async getHistoryList() {
      try {
        this.setData({
          searchText: '',
          historyList: [],
          fetchLoading: true,
        })
        const res = await api.historyTopicList({
          uid: this.data.uid,
        })
        if (res.topic_list && res.topic_list.length) {
          this.setHistoryDataGroup(res.topic_list)
        }
      } catch(e){}
      this.data.cacheHistoryList = JSON.parse(JSON.stringify(this.data.historyList))
      this.setData({
        fetchLoading: false,
        historyList: this.data.historyList,
      })
    },
    clearHistoryList(){
      this.setData({
        fetchLoading: false,
        historyList: [],
      })
    },
    clearSearch() {
      console.log('999999999999999999999')
      this.data.historyList = JSON.parse(JSON.stringify(this.data.cacheHistoryList))
      this.setData({
        searchText: '',
        historyList: this.data.historyList
      })
    },
    async searchHistory() {
      this.data.cacheHistoryList = JSON.parse(JSON.stringify(this.data.historyList))
      this.data.historyList = []
      this.setData({
        fetchLoading: true,
      })
      try {
        const res = await api.searchTopic({
          uid: this.data.uid,
          search_text: this.data.searchText
        })
        if (res.search_results && res.search_results.length) {
          res.search_results.sort((a, b) => {
            return new Date(b.last_chat_time).getTime() - new Date(a.last_chat_time).getTime()
          })
          this.setHistoryDataGroup(res.search_results)
        }
      } catch(e){
      }
      this.setData({
        historyList: this.data.historyList,
        fetchLoading: false,
      })
    },
    enterTopic(e: BaseEvent){
      const item = e.currentTarget.dataset.item;
      app.globalData.topic = {
        topic_id: item.topic_id,
        topic_name: item.topic_name,
      }
      this.setData({
        topic: app.globalData.topic,
      })
      this.triggerEvent('enterTopic', app.globalData.topic)
    },
    closePage(){
      this.triggerEvent('close')
    }
  }
})